import axios from "axios";
import router from "@/router";
// import { Message, Loading } from "element-plus";
import qs from "qs";
import { ElNotification } from "element-plus";
// 超时时间
axios.defaults.timeout = 100000;
// 跨域请求，允许保存cookie
axios.defaults.withCredentials = true;
// axios.defaults.headers = { "Content-Type": "application/json; charset=utf-8" };
axios.defaults.headers["Content-Type"] =
  "application/x-www-form-urlencoded; charset=utf-8";
// 非生产环境 && 开启代理, 接口前缀统一使用[/api]前缀做代理拦截!

const BASE_URL =
  import.meta.env.MODE === "production"
    ? "http://meipaimy.sxkj.fun:8021/"
    : "/baseApi";
// 对面暴露的基础请求路径
axios.BASE_URL = BASE_URL;

/**
 * 请求拦截
 */
let loading;
axios.interceptors.request.use(
  (config) => {
    let showLoading = false;
    if (config.loading === true) {
      showLoading = true;
    }
    if (showLoading) {
      // loading = Loading.service({
      //   text: config.loadingText || "Loading...",
      //   spinner: "el-icon-loading",
      //   background: "rgba(0, 0, 0, 0.7)",
      // });
    }
    // 请求头带上token
    // config.headers.token = store.state.user.user.token;
    if (config.method == "get" || config.method == "delete") {
      config.paramsSerializer = function (params) {
        return qs.stringify(params, { arrayFormat: "repeat" });
      };
    }
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

/**
 * 响应拦截
 */
axios.interceptors.response.use(
  (response) => {
    if (loading) {
      loading.close();
    }
    
    let rightCode = [0];
    if ( !rightCode.includes(response.data.Status) ) {
      if(!response.request.responseURL.includes('PostUserInfoFromToken')) {
        ElNotification({
          message: response.data.Message,
          type: "error",
          title:"异常"
        });
      }
      
    }
    return response;
  },
  (error) => {
    if (loading) {
      loading.close();
    }
    if(error.request.responseURL.includes('PostUserInfoFromToken')) {
      return
    }
    if (error.response.status === 401) {
      // 超时自动刷新
    } else if (error.response.status === 402 || error.response.status === 403) {
      // 402 未登录或者refresh token过时， 403 账号在其他地方登录
      ElNotification({
        message: error.response.data.Message || error.response.data.exception,
        type: "error",
        showClose: true,
        dangerouslyUseHTMLString: true,
        duration: 3000,
        customClass: "zZindex",
      });
    } else if (error.response.status === 404) {
      // 路径找不到
      ElNotification({
        message: "404，路径找不到" + ":" + error.response.data.Message,
        type: "error",
        showClose: true,
        duration: 3000,
      });
    } else if (error.response.status === 504) {
      ElNotification({
        message: "网络连接错误" + ":" + error.response.data.Message,
        type: "error",
        showClose: true,
        duration: 3000,
        customClass: "zZindex",
      });
    } else if (error.response.status === 502) {
      ElNotification({
        message: "服务器连接失败" + ":" + "502",
        type: "error",
        showClose: true,
        duration: 3000,
        customClass: "zZindex",
      });
    } else {
      ElNotification({
        message:
          error.response.data.Message ||
          error.response.data ||
          error.response.data.exception ||
          error.response ||
          error,
        type: "error",
        showClose: true,
        duration: 5000,
        customClass: "zZindex",
      });
    }

    return Promise.reject(error);
  }
);

export default {
  get(url, param) {
    return new Promise((resolve, reject) => {
      axios({
        method: "get",
        url: BASE_URL + url,
        params: param,
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
        },
      })
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },
  delete(url, param) {
    return new Promise((resolve, reject) => {
      axios({
        method: "delete",
        url: BASE_URL + url,
        params: param,
        headers: {
          "Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
        },
      })
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },
  posturl(url, param, type) {
    return new Promise((resolve, reject) => {
      axios({
        method: "post",
        url: BASE_URL + url,
        params: param,
        headers: {
          "Content-Type": "application/x-www-form-urlencoded;",
        },
      })
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },
  // post请求
  post(url, param, type) {
    return new Promise((resolve, reject) => {
      axios({
        method: "post",
        url: BASE_URL + url,
        data: !type ? param : "",
        params: type ? param : "",
        headers: {
          "Content-Type": "application/json; charset=utf-8",
        },
      })
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },
  // post下载blob
  postBlob(url, param, type) {
    return new Promise((resolve, reject) => {
      axios({
        method: "post",
        url: BASE_URL + url,
        data: !type ? param : "",
        params: type ? param : "",
        headers: {
          "Content-Type": "application/json; charset=utf-8",
        },
        responseType: "blob",
      })
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },
  // get下载blob
  getBlob(url, param, type) {
    return new Promise((resolve, reject) => {
      axios({
        method: "get",
        url: BASE_URL + url,
        params: type ? param : "",
        headers: {
          "Content-Type": "application/x-www-form-urlencoded",
        },
        responseType: "blob",
      })
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },
  // form-data
  postform(url, param, files) {
    let params = new FormData();
    for (let x of files) {
      params.append("file", x);
    }
    for (let i in param) {
      params.append(i, param[i]);
    }
    return new Promise((resolve, reject) => {
      axios({
        method: "post",
        url: BASE_URL + url,
        data: params,
        headers: {
          "Content-Type": "multipart/form-data",
        },
      })
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },

  // post请求
  put(url, param, type) {
    return new Promise((resolve, reject) => {
      axios({
        method: "put",
        url: BASE_URL + url,
        data: !type ? param : "",
        params: type ? param : "",
        headers: {
          "Content-Type": "application/json; charset=utf-8",
        },
      })
        .then((res) => {
          resolve(res);
        })
        .catch((err) => {
          reject(err);
        });
    });
  },
};
